interface KanbanCard {
  title: string;
  oldName: string;
  oldValue: number | string;
  newName: string;
  newValue: number | string;
}

interface KanbanData {
  ds?: Record<string, any>[];
  ds1?: Record<string, any>[];
  ds2?: Record<string, any>[];
  ds3?: Record<string, any>[];
  ds4?: Record<string, any>[];
}

type KanbanType = '销售' | '仓库' | '采购';

class KanbanService {
  // 外部调用方法
  getInitByKanban(type: KanbanType, data: KanbanData): KanbanCard[] {
    switch (type) {
      case '销售':
        return this.initXiaoshou(data);
      case '仓库':
        return this.initCangku(data);
      case '采购':
        return this.initCaigou(data);
      default:
        return [];
    }
  }

  // 销售处理
  private initXiaoshou(data: KanbanData): KanbanCard[] {
    if (uni.$utils.isEmpty(data)) return [];

    const { ds = [], ds1 = [], ds2 = [], ds3 = [], ds4 = [] } = data;

    return [
      {
        title: '销售数量',
        oldName: '去年销售量',
        oldValue: ds[0]?.OldQty ?? 0,
        newName: '今年销售量',
        newValue: ds[0]?.Qty ?? 0,
      },
      {
        title: '销售金额（万元）',
        oldName: '去年销售额',
        oldValue: ds1[0]?.OldMoney ?? 0,
        newName: '今年销售额',
        newValue: ds1[0]?.Money ?? 0,
      },
      {
        title: '销售出库',
        oldName: '去年出库量',
        oldValue: ds2[0]?.OldFQty ?? 0,
        newName: '今年出库量',
        newValue: ds2[0]?.FQty ?? 0,
      },
      {
        title: '回款金额（万元）',
        oldName: '去年回款',
        oldValue: ds3[0]?.OldBCMoney ?? 0,
        newName: '今年回款',
        newValue: ds3[0]?.BCMoney ?? 0,
      },
      {
        title: '待收金额（万元）',
        oldName: '去年待收金额',
        oldValue: ds4[0]?.OldWSKMoney ?? 0,
        newName: '今年待收金额',
        newValue: ds4[0]?.WSKMoney ?? 0,
      },
    ];
  }

  // 仓库处理
  private initCangku(data: KanbanData): KanbanCard[] {
    if (uni.$utils.isEmpty(data)) return [];

    const { ds1 = [], ds2 = [], ds3 = [], ds4 = [] } = data;

    return [
      {
        title: '期初存货数量',
        oldName: '实际值',
        oldValue: ds1[0]?.FQCNum ?? 0,
        newName: '同期值',
        newValue: ds2[0]?.FQCNum ?? 0,
      },
      {
        title: '本期入库',
        oldName: '实际值',
        oldValue: ds3[0]?.FQty ?? 0,
        newName: '同期值',
        newValue: ds3[0]?.OldFQty ?? 0,
      },
      {
        title: '本期出库',
        oldName: '实际值',
        oldValue: ds4[0]?.FQty ?? 0,
        newName: '同期值',
        newValue: ds4[0]?.OldFQty ?? 0,
      },
      {
        title: '期末存货数量',
        oldName: '实际值',
        oldValue: ds1[0]?.FDQNum ?? 0,
        newName: '同期值',
        newValue: ds2[0]?.FDQNum ?? 0,
      },
    ];
  }

  // 采购处理
  private initCaigou(data: KanbanData): KanbanCard[] {
    if (uni.$utils.isEmpty(data)) return [];

    const { ds = [], ds1 = [], ds2 = [] } = data;

    return [
      {
        title: '采购申请',
        oldName: '上月申请数',
        oldValue: ds[0]?.OldQty ?? 0,
        newName: '本月申请数',
        newValue: ds[0]?.Qty ?? 0,
      },
      {
        title: '采购订单',
        oldName: '上月采购数',
        oldValue: ds1[0]?.OldQty ?? 0,
        newName: '本月采购数',
        newValue: ds1[0]?.Qty ?? 0,
      },
      {
        title: '采购金额(万元)',
        oldName: '上月采购额',
        oldValue: ds2[0]?.OldMoney ?? 0,
        newName: '本月采购额',
        newValue: ds2[0]?.Money ?? 0,
      },
    ];
  }
}

// 单例导出，避免多次实例化
const kanbanService = new KanbanService();
export default kanbanService;
